package ru.jexp.rcp.supply.beans.zt;

import java.util.List;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

import ru.jexp.izdelie.zakaznaaspecifikacia.ZakaznaaSpecifikaciaLite;
import ru.jexp.rcp.supply.beans.zs.JExpTableLabelProviderZs;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.swtext.factory.JExpLayoutFactory;
import ru.jexp.rcp.supply.viewer.JExpContentProvider;
import ru.jexp.rcp.supply.viewer.JExpTableLabelProvider;
import ru.jexp.rcp.supply.viewer.JExpTableViewerComparator;

public class WizardPageChooseZs extends WizardPage implements
		ICheckStateListener {

	private CheckboxTableViewer viewer;
	private WizardPresentorZt model;

	public WizardPageChooseZs(WizardPresentorZt model) {
		super(JExpSupplyMessages.Zt_wizard_choosezs_pagename);
		setTitle(JExpSupplyMessages.Zt_wizard_choosezs_pagename);
		this.model = model;
	}

	@Override
	public void createControl(Composite composite) {
		Composite parent = new Composite(composite, SWT.NONE);
		parent.setLayout(new GridLayout());
		viewer = CheckboxTableViewer.newCheckList(parent, SWT.FULL_SELECTION);
		viewer.setContentProvider(new JExpContentProvider());
		viewer.setLabelProvider(new JExpTableLabelProviderZs());
		viewer.setComparator(new JExpTableViewerComparator());
		viewer.getTable().setLinesVisible(true);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLayoutData(JExpLayoutFactory.gdFillBoth());
		viewer.addCheckStateListener(this);

		createColumn((JExpTableLabelProvider) viewer.getLabelProvider());
		setControl(parent);
	}

	private void createColumn(JExpTableLabelProvider provider) {
		for (int i = 0; i < provider.getColumnsSize(); i++) {
			final TableColumn tableColumn = new TableColumn(viewer.getTable(),
					SWT.NONE);
			tableColumn.setWidth(200);
			tableColumn.setText(provider.getColumnName(i));
			tableColumn.setToolTipText(provider.getColumnName(i));
			tableColumn.setMoveable(true);
			tableColumn.setResizable(true);
			tableColumn.pack();
			tableColumn.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					JExpTableViewerComparator comparetor = (JExpTableViewerComparator) viewer
							.getComparator();
					comparetor.setCurrentColumn(tableColumn.getText());
					viewer.getTable().setSortDirection(
							comparetor.getDirection());
					viewer.getTable().setSortColumn(tableColumn);
					viewer.refresh();
				}
			});
		}
	}

	@Override
	public void checkStateChanged(CheckStateChangedEvent event) {
		if (isEmptyChecked()) {
			setMessage(JExpSupplyMessages.Zt_wizard_choosezs_error, WARNING);
		} else {
			setMessage(null);
		}
		model.configureRequirment(viewer.getCheckedElements());
		setPageComplete(!isEmptyChecked());
		getContainer().updateButtons();				
	}

	public void setZsList(List<ZakaznaaSpecifikaciaLite> zsList) {
		viewer.setInput(zsList);
		TableColumn[] columns = viewer.getTable().getColumns();
		for (TableColumn column : columns) {
			column.pack();
		}
	}

	public boolean canFinish() {
		return !isEmptyChecked();
	}

	private boolean isEmptyChecked() {
		return viewer.getCheckedElements() == null
				|| viewer.getCheckedElements().length == 0;
	}
}
